#!/bin/bash
 
# 检查nginx进程是否存在
A=`ps -ef | grep nginx -w | grep -v grep | wc -l`
 
# 如果nginx进程不存在，尝试启动nginx
if [ $A -eq 0 ]; then
    # 检查nginx可执行文件是否存在
    if [ -x "/usr/local/nginx/sbin/nginx" ]; then
        /usr/local/nginx/sbin/nginx
        sleep 2
 
        # 再次检查nginx是否启动成功
        if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
            echo "Nginx failed to start, stopping keepalived..."
            systemctl stop keepalived
        else
            echo "Nginx started successfully."
        fi
    else
        echo "Nginx executable not found at /usr/local/nginx/sbin/nginx, stopping keepalived..."
        systemctl stop keepalived
    fi
else
    echo "Nginx is already running."
fi